﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace StickyNote.Core
{
    public enum E_Sections
    {
        业务,
        系统设置,
        便签显示属性
    }

    public enum E_Keys
    {
        便签
    }

    internal class IniHelper
    {
        private static string fSaveFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\StickyNote\\configs.ini";

        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(string section, string key, string value, string filepath);

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder returnvalue, int buffersize, string filepath);

        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static string GetValue(string section, string key)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (File.Exists(fSaveFilePath))
            {
                GetPrivateProfileString(section, key, string.Empty, stringBuilder, 1024, fSaveFilePath);
            }

            return stringBuilder.ToString() ?? string.Empty;
        }

        /// <summary>
        /// 设置配置
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetValue(string section, string key, string value)
        {
            if (!Directory.Exists(Path.GetDirectoryName(fSaveFilePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fSaveFilePath));
            }

            WritePrivateProfileString(section, key, value, fSaveFilePath);
        }
    }
}
